package main

import (
	"fmt"
	"strconv"
)

// 初版
func calPoints_(operations []string) int {
	resPoints := make([]int, 2)
	var res int = 0
	for _, str := range operations {
		len := len(resPoints)
		if str == "+" {
			val := resPoints[len-1] + resPoints[len-2]
			resPoints = append(resPoints, val)
		} else if str == "D" {
			val := resPoints[len-1] * 2
			resPoints = append(resPoints, val)
		} else if str == "C" {
			resPoints = resPoints[:len-1]
		} else {
			val, err := strconv.Atoi(str)
			if err != nil {
				panic("程序错误")
			}

			resPoints = append(resPoints, val)
		}
	}

	for _, val := range resPoints {
		res += val
	}

	return res
}

// 代码风格增强版
func calPoints(operations []string) (res int) {
	// 得分切片
	resPoints := make([]int, 0)
	for _, op := range operations {
		// 分数index
		len := len(resPoints)
		switch op {
		case "+":
			val := resPoints[len-1] + resPoints[len-2]
			resPoints = append(resPoints, val)
			res += val
		case "D":
			val := resPoints[len-1] * 2
			resPoints = append(resPoints, val)
			res += val
		case "C":
			res -= resPoints[len-1]
			resPoints = resPoints[:len-1]
		default:
			val, _ := strconv.Atoi(op)
			resPoints = append(resPoints, val)
			res += val
		}
	}
	return
}

func main() {
	points := []string{"5", "-2", "4", "C", "D", "9", "+", "+"}
	res := calPoints(points)
	fmt.Printf("res:%d\n", res)
}
